package com.cty.greedy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/*
 *
 *
 * 贪心算法解决电台问题
 *
 * */
public class Greedy {
    public static void main(String[] args) {
        HashMap<String, HashSet> radioStation = new HashMap<>();
        ArrayList<String> selects = new ArrayList<>();
        HashSet<String> hashSet1 = new HashSet<>();
        hashSet1.add("北京");
        hashSet1.add("上海");
        hashSet1.add("天津");

        HashSet<String> hashSet2 = new HashSet<>();
        hashSet2.add("广州");
        hashSet2.add("北京");
        hashSet2.add("深圳");

        HashSet<String> hashSet3 = new HashSet<>();
        hashSet3.add("成都");
        hashSet3.add("上海");
        hashSet3.add("杭州");

        HashSet<String> hashSet4 = new HashSet<>();
        hashSet4.add("上海");
        hashSet4.add("天津");

        HashSet<String> hashSet5 = new HashSet<>();
        hashSet5.add("杭州");
        hashSet5.add("大连");


        radioStation.put("K1",hashSet1);
        radioStation.put("K2",hashSet2);
        radioStation.put("K3",hashSet3);
        radioStation.put("K4",hashSet4);
        radioStation.put("K5",hashSet5);

        HashSet<String> allAreas = new HashSet<>();
        allAreas.add("北京");
        allAreas.add("天津");
        allAreas.add("上海");
        allAreas.add("杭州");
        allAreas.add("大连");
        allAreas.add("广州");
        allAreas.add("深圳");
        allAreas.add("成都");

        HashSet<Object> tempSet = new HashSet<>();
        String maxKey = null;
        while (allAreas.size() > 0) {
            maxKey = null;
            for (String key : radioStation.keySet()) {
                tempSet.clear();
                tempSet.addAll(radioStation.get(key));
                tempSet.retainAll(allAreas);
                if (tempSet.size() > 0 &&
                        (maxKey == null || tempSet.size() > radioStation.get(maxKey).size())) {
                    maxKey = key;
                }
            }
            if (maxKey != null) {
                selects.add(maxKey);
                allAreas.removeAll(radioStation.get(maxKey));
            }
        }
        System.out.println("结果是" + selects);
    }
}
